package com.fanqwz.hexieshe.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.fanqwz.hexieshe.R;
import com.fanqwz.hexieshe.activity.BaseActivity;
import com.fanqwz.hexieshe.adapter.CommentAdapter;
import com.fanqwz.hexieshe.model.ArticleModel;
import com.fanqwz.hexieshe.model.CommentModel;
import com.fanqwz.hexieshe.presenter.ArticlePresenter;
import com.fanqwz.hexieshe.view.ArticleView;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by fanqw on 2017/6/30.
 */

public class ArticleFragment extends BaseFragment implements ArticleView {
    @Bind(R.id.progress_bar)
    ProgressBar progressBar;
    @Bind(R.id.webview)
    WebView webView;
    @Bind(R.id.article_content)
    ViewGroup content;
    @Bind(R.id.comment_list)
    RecyclerView commentList;

    @Inject
    ArticlePresenter articlePresenter;
    @Inject
    ActionBar actionBar;
    @Inject
    CommentAdapter commentAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((BaseActivity) getActivity()).getComponent().inject(this);
        articlePresenter.setArticleView(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.article_fragment, container, false);
        ButterKnife.bind(this, view);

        commentList.setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                //prevent nested scroll with the outer scrollerview
                return false;
            }
        });
        commentList.setAdapter(commentAdapter);
        commentList.addItemDecoration(new DividerItemDecoration(getActivity(), OrientationHelper.VERTICAL));

        WebSettings settings = webView.getSettings();
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

        articlePresenter.create();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getArguments() != null) {
            articlePresenter.setArticleID(getArguments().getLong("id"));
        }
        articlePresenter.show();
    }

    @Override
    public void onPause() {
        articlePresenter.hide();
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        ButterKnife.unbind(this);
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        articlePresenter.destroy();
        super.onDestroy();
    }

    @Override
    public void prepare() {
        content.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void showArticle(ArticleModel article) {
        content.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
        actionBar.setTitle(article.getTitle());
        if (article != null)
            webView.loadData(article.getContent(), "text/html; charset=UTF-8", null);
    }

    @Override
    public void showComments(List<CommentModel> commentModels) {
        commentAdapter.setComments(commentModels);
        commentAdapter.notifyDataSetChanged();
    }
}
